package com.sys.dto;

import com.sys.dto.idto.IOrderDTO;
import com.sys.entity.OrdersEntity;
import jakarta.persistence.*;

import java.util.List;

public class OrderDTO implements IOrderDTO {
    @Override
    public void save(OrdersEntity entity) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        em.persist(entity);
        em.getTransaction().commit();
    }

    @Override
    public void delete(Integer id) {

    }

    @Override
    public OrdersEntity update(OrdersEntity entity, Integer oid) {
        return null;
    }

    @Override
    public OrdersEntity findById(Integer id) {
        return null;
    }

    @Override
    public OrdersEntity changePassword(Integer id, String password) {
        return null;
    }

    @Override
    public List<OrdersEntity> findByProperty(String propertyName, Object value) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();
        // 使用查询语句来查找用户
        TypedQuery<OrdersEntity> query = em.createQuery(
                "SELECT u FROM OrdersEntity u WHERE u."+ propertyName + "= : " + propertyName,
                OrdersEntity.class
        );
        query.setParameter(propertyName, value);
        List<OrdersEntity> ordersEntities;
        try{
            ordersEntities = query.getResultList();
            return ordersEntities;
        }catch (NoResultException e){
            System.out.println("没有找到用户");
            return null;
        }finally {
            em.close();
        }
    }

    @Override
    public List<OrdersEntity> findByName(String name) {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

        // 使用查询语句来查找用户
        TypedQuery<OrdersEntity> query = em.createQuery(
                "SELECT u FROM OrdersEntity u WHERE u.producename = :producename",
                OrdersEntity.class
        );
        query.setParameter("producename", name);

        List<OrdersEntity> ordersEntities;
        try {
            ordersEntities = query.getResultList();
            return ordersEntities;
        } catch (NoResultException e) {
            System.out.println("没有找到用户");
            return null;
        }
    }

    @Override
    public List<OrdersEntity> findByType(Integer oType) {

        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

        // 使用查询语句来查找用户
        TypedQuery<OrdersEntity> query = em.createQuery(
                "SELECT u FROM OrdersEntity u WHERE u.oType = :oType",
                OrdersEntity.class
        );
        query.setParameter("oType", oType);

        List<OrdersEntity> ordersEntities;
        try {
            ordersEntities = query.getResultList();
            return ordersEntities;
        } catch (NoResultException e) {
            System.out.println("没有找到用户");
            return null;
        }
    }

    @Override
    public List<OrdersEntity> findAll() {
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("default");
        EntityManager em = emf.createEntityManager();

// 使用查询语句来查找所有用户
        TypedQuery<OrdersEntity> query = em.createQuery(
                "SELECT u FROM OrdersEntity u",
                OrdersEntity.class
        );

        List<OrdersEntity> ordersEntities = query.getResultList();

        for (OrdersEntity ordersEntity : ordersEntities) {
            System.out.println("找到的用户: " + ordersEntity.getProducename());
        }

        return ordersEntities;
    }
}
